package com.dhj.optionalTest;

import com.dhj.streamTest.entity.Author;

import java.util.Objects;
import java.util.Optional;

public class Demo2 {

    public static void main(String[] args) {
        Author author = getAuthor();

        Optional<Author> optionalAuthor = Optional.ofNullable(author);
        optionalAuthor.ifPresent(author1 -> System.out.println(author1.getName()));


        Optional<Author> author2 = getAuthor2();
        author2.ifPresent(obj -> System.out.println(obj.getName()));


        Optional<Author> optional2 = Optional.of(author);
        optional2.ifPresent(author1 -> System.out.println(author1.getName()));

    }


    public static Author getAuthor() {
        Author author = new Author(5L, "林则徐1", 45, "苟利国家生死以", null);
        return author;
    }


    public static Optional<Author> getAuthor2() {
        Author author = new Author(5L, "林则徐2", 45, "苟利国家生死以", null);
        return Optional.ofNullable(author);
    }

    public static Optional<Author> getAuthor3() {
        Author author = new Author(5L, "林则徐2", 45, "苟利国家生死以", null);
        if (Objects.isNull(author)) {
            return Optional.empty();
        } else {
            return Optional.of(author);
        }
    }

}
